package de.sisoft.gcjack.communication;

import org.json.JSONException;
import org.json.JSONObject;

import de.sisoft.gcjack.model.GeoCache;
import de.sisoft.gcjack.model.User;

public class ResponseBuilder {

    public GeoCache buildGeoCacheDetailsFromJson(JSONObject jObj,
	    GeoCache geocache) {
	if (geocache == null) {
	    geocache = new GeoCache();
	}

	try {
	    geocache.setName(jObj.getString(Constants.TAG_GCNAME));
	    geocache.setGcCode(jObj.getString(Constants.TAG_GCCODE));
	    geocache.setOwner(new User(jObj.getString(Constants.TAG_GCOWNER)));
	    geocache.setType(jObj.getString(Constants.TAG_GCTYPE));
	    geocache.setDifficulty(toFloat(jObj.getDouble(Constants.TAG_GCDIFFICULTY)));
	    geocache.setTerrain(toFloat(jObj.getDouble(Constants.TAG_GCTERRAIN)));
	    geocache.setHidden(jObj.getString(Constants.TAG_GCHIDDEN));
	} catch (JSONException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	// do some stuff here
	return geocache;
    }

    public GeoCache buildGeoCacheVotingsFromJson(JSONObject jObj,
	    GeoCache geocache) {
	if (geocache == null) {
	    geocache = new GeoCache();
	}
	try {
		geocache.setNumberVotes(jObj.getInt(Constants.TAG_NUMBERVOTINGS));
	    geocache.setAvgCoordQuality(toFloat(jObj
		    .getDouble(Constants.TAG_AVGCOORDQUALITY)));
	    geocache.setAvgCreativity(toFloat(jObj
		    .getDouble(Constants.TAG_AVG_CREATIVITY)));
	    geocache.setAvgDifficulty(toFloat(jObj
		    .getDouble(Constants.TAG_AVG_DIFFICULTY)));
	    geocache.setAvgRecommendation(jObj
		    .getInt(Constants.TAG_RECOMMENDATION));
	    geocache.setAvgTerrain(toFloat(jObj.getDouble(Constants.TAG_AVG_TERRAIN)));
	    geocache.setAvgWowFactor(toFloat(jObj
		    .getDouble(Constants.TAG_AVG_WOWFACTOR)));
	} catch (JSONException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	// do some stuff here
	return geocache;
    }

    public User buildUser(JSONObject jObj) {

	// do some stuff here
	return new User();
    }

    
    private float toFloat(Double d){
	return d.floatValue();
    }
}
